package com.example.mylibrarybase.yumengmeng03.utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.yumengmeng03.bean.itemClass_03;

import java.util.ArrayList;

/**
 * Created by 35876 于萌萌
 * 创建日期: 16:17 . 2016年10月07日
 * 描述:
 * <p/>
 * <p/>
 * 备注:
 */
public class UtiilsCodes_3 {
    public static Activity mActivity;

    public interface CallBack{
        void checed(itemClass_03 tag);
    }



    public static void AutoCodes(Activity activity , ArrayList<itemClass_03> itemClasses, LinearLayout linearLayout , final CallBack callBack){
       final ArrayList<RadioButton>radioButtons= new ArrayList<>();
        linearLayout.removeAllViews();
        mActivity = activity;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        RadioGroup radioGroup = new RadioGroup(activity);
        RadioGroup.LayoutParams radioGrouplayoutParams = new RadioGroup.LayoutParams(0, RadioGroup.LayoutParams.WRAP_CONTENT,1);
        RadioButton radioButton;
        LogUtils.i("数据集合内容是"+itemClasses);
        for (int i = 0; i <itemClasses.size(); i++) {
            itemClass_03 itemClass = itemClasses.get(i);
            radioButton = new RadioButton(activity);
            radioGrouplayoutParams.setMargins(20,20,20,20);
            radioButton.setLayoutParams(radioGrouplayoutParams);
            radioButton.setBackgroundResource(R.drawable.abv_3);
            radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
            radioButton.setText(itemClass.name);
            if (i==0){
                radioButton.setChecked(true);
                radioButton.setTextColor(mActivity.getResources().getColor(R.color.red));
            }else{

                radioButton.setTextColor(mActivity.getResources().getColor(R.color.black));
            }

            radioButton.setTag(itemClass);
            radioButton.setGravity(Gravity.CENTER);
            radioButtons.add(radioButton);
            radioGroup.addView(radioButton);

            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RadioButton button = (RadioButton) view;
                    itemClass_03 tag = (itemClass_03) button.getTag();
                    LogUtils.i("我点的对象的内容"+tag);
                    for (int i = 0; i < radioButtons.size(); i++) {
                        RadioButton button1 = radioButtons.get(i);
                        if (button==button1){
                            button1.setTextColor(mActivity.getResources().getColor(R.color.red));
                        }else{
                            button1.setTextColor(mActivity.getResources().getColor(R.color.black));
                        }
                    }
                    callBack.checed(tag);
                }
            });

        }
        radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        radioGroup.setLayoutParams(layoutParams);
        //  callBack.checed(compoundButton,b,tag);



        linearLayout.addView(radioGroup);
//        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                LogUtils.i("我点的号码是"+i);
//                int ii= i-1;
//                RadioButton childAtRadioButton = (RadioButton) radioGroup.getChildAt(ii);
//                itemClass_03 tag = (itemClass_03) childAtRadioButton.getTag();
//                for (int y = 0; y < radioGroup.getChildCount(); y++) {
//                    RadioButton childAtRadioButtony = (RadioButton) radioGroup.getChildAt(y);
//                    if (childAtRadioButtony==childAtRadioButton){
//                        childAtRadioButtony.setTextColor(mActivity.getResources().getColor(R.color.red));
//                    }else{
//                        childAtRadioButtony.setTextColor(mActivity.getResources().getColor(R.color.black));
//                    }
//                }
//
//
//
//                LogUtils.i("取出来的数据是"+tag);
//                callBack.checed(tag,ii);
//            }
//        });
    }
}
